package com.zw.transition.services;

import com.zw.transition.entity.User;
import com.zw.transition.mapper.UserMapper;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @Description:
 * @Author: China.sgl
 * @Date: 2019/9/16 09:11
 */
@Service
@SuppressWarnings("all")
public class UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LogService logService;
    /** 上传路径 */
    @Value("${user.upload.path}")
    private String filePath;
    /**
     * 登录
     *
     * @param user
     * @return
     */
    public User login(User user) throws IOException {
        String sourcePwd = user.getPwd();
        user.setPwd(new SimpleHash("MD5", user.getPwd(), ByteSource.Util.bytes(user.getAccount()), 8).toString());
        user.setStatus(1);
        User loginUser = userMapper.findByAccountAndPasswordAndStatus(user);
        if (loginUser != null) {
            loginUser.setPwd(sourcePwd);
        }
        return loginUser;
    }

    /**
     * 根据工作证号获取用户
     *
     * @param account
     * @return
     */
    public User getTransUserByAccount(String account) {
        return userMapper.getTransUserByAccount(account);
    }

    /**
     * 更新头像
     * @param user
     * @return
     */
    public int updateUserAvatarByPrimaryKey(User user) {
        return userMapper.updateUserAvatarByPrimaryKey(user);
    }

    /**
     * 更新密码
     * @param databaseUser
     * @return
     */
    public boolean updateUserPwd(User databaseUser) {
        boolean result = false;
        try{
            userMapper.updateUserPwd(databaseUser);
            result = true;
        }catch (Exception e){
            result = false;
        }
        return result;
    }
}
